require("../../@babel/runtime/helpers/Objectvalues");

var a = getApp();

Page({
    data: {
        language: "",
        coupon_list: [],
        page: 1,
        goload: !0,
        total_page: 0,
        loading: !0,
        allLoaded: !1,
        isLoadMore: !0,
        types: [],
        typesIndex: 0,
        kFootshow: !0,
        coupon_form: "",
        visibleIntegralPopup: !1,
        exchangeForm: {},
        integralPluginName: ""
    },
    onLoad: function(a) {
        this._initData(), this._getNetData();
    },
    onReady: function() {
        var a = wx.getStorageSync("langIndex");
        this.setData({
            language: a.en
        });
    },
    gocouponStore: function() {
        wx.redirectTo({
            url: "/packageA/member/coupon_v2/coupon_v2"
        });
    },
    tapurl: function(a) {
        console.log(a.currentTarget.dataset.link);
        var e = a.currentTarget.dataset.link;
        wx.navigateTo({
            url: e
        });
    },
    setTypesIndex: function(a) {
        var e = a.currentTarget.dataset.index;
        e != this.data.typesIndex && (this.setData({
            typesIndex: e
        }), this._getNetData());
    },
    onShow: function() {},
    onPullDownRefresh: function() {
        this._initData(), this._getNetData();
    },
    onReachBottom: function() {
        this.data.isLoadMore ? this._getMoreData(this.data.page) : console.log("没有更多数据");
    },
    onShareAppMessage: function() {},
    _initData: function() {
        this.setData({
            coupon_list: [],
            page: 1,
            goload: !0,
            total_page: 0,
            loading: !0,
            allLoaded: !1,
            isLoadMore: !0
        });
    },
    _getNetData: function() {
        var e = this, t = a.getNetAddresss("coupon.member-coupon.coupons-for-member");
        1 == a.globalData.store_alone_temp && (t = a.getNetAddresss("plugin.store-alone-temp.frontend.coupon.coupons-for-member")), 
        t += "&page=" + e.data.page, t += "&coupon_type=" + e.data.typesIndex, a._getNetWork({
            url: t,
            success: function(a) {
                var t = a.data;
                if (1 == t.result) {
                    var o = t.data;
                    console.log(o);
                    for (var n = 0; n < o.data.data.length; n++) {
                        var s = o.data.data[n];
                        s.isIntegralExchange = 0 != s.is_integral_exchange_coupon && s.exchange_coupon_integral > 0, 
                        "平台商品" == s.coupon_type_name ? s.type = 1 : "兑换券" == s.coupon_type_name ? s.type = 2 : "分类商品" == s.coupon_type_name ? s.type = 3 : "指定商品" == s.coupon_type_name ? s.type = 4 : "酒店商品" == s.coupon_type_name ? s.type = 5 : "门店商品" == s.coupon_type_name ? s.type = 6 : "商品和门店" == s.coupon_type_name && (s.type = 8);
                    }
                    e.setData({
                        total_page: t.data.data.last_page,
                        coupon_list: o.data.data,
                        types: o.search_array instanceof Object ? Object.values(o.search_array) : o.search_array,
                        slide_shows: o.slide_shows,
                        integralPluginName: o.integral_plugin_name ? o.integral_plugin_name : "消费积分"
                    }), e.data.page >= t.data.data.last_page && e.setData({
                        isLoadMore: !1
                    }), t.data.coupon_form && e.setData({
                        coupon_form: t.data.coupon_form
                    });
                } else e.setData({
                    coupon_list: t.data
                }), wx.showToast({
                    icon: "none",
                    title: t.msg,
                    duration: 1e3
                });
                wx.stopPullDownRefresh();
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    selectedcoupon: function(e, t, o) {
        if (!t.isIntegralExchange) {
            if (!a._isTextEmpty(this.data.coupon_form)) return void wx.navigateTo({
                url: "/packageC/diyform/diyform?scene=" + this.data.coupon_form + "&tag=coupon"
            });
            if (2 == e) return void wx.navigateTo({
                url: "/packageE/team_sell/getCouponShare/getCouponShare?id=" + t.id
            });
        }
        var n = this, s = a.getNetAddresss("coupon.member-coupon.get-coupon");
        s += "&coupon_id=" + t.id, t.isIntegralExchange && this.data.exchangeForm.couponTotal > 0 && (s += "&exchange_total=" + this.data.exchangeForm.couponTotal), 
        a._getNetWork({
            url: s,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    var s = e.data, p = n.data.coupon_list;
                    s.isIntegralExchange = 0 != s.is_integral_exchange_coupon && s.exchange_coupon_integral > 0, 
                    p[o] = s;
                    for (var i = 0; i < p.length; i++) "平台商品" == p[i].coupon_type_name ? p[i].type = 1 : "兑换券" == p[i].coupon_type_name ? p[i].type = 2 : "分类商品" == p[i].coupon_type_name ? p[i].type = 3 : "指定商品" == p[i].coupon_type_name ? p[i].type = 4 : "酒店商品" == p[i].coupon_type_name ? p[i].type = 5 : "门店商品" == p[i].coupon_type_name ? p[i].type = 6 : "商品和门店" == p[i].coupon_type_name && (p[i].type = 8);
                    wx.showToast({
                        title: t.isIntegralExchange ? "兑换成功" : "领取成功",
                        icon: "none",
                        duration: 1e3
                    }), n.setData({
                        coupon_list: p
                    });
                } else wx.showModal({
                    title: e.data.reason ? e.msg : "",
                    content: e.data.reason ? e.data.reason : e.msg,
                    showCancel: !1
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    selectedcouponBtn: function(a) {
        wx.navigateTo({
            url: "/packageE/team_sell/getCouponShare/getCouponShare?id=" + item.id
        });
    },
    _getMoreData: function(e) {
        if (this.data.page != this.data.total_page) {
            var t = this, o = a.getNetAddresss("coupon.member-coupon.coupons-for-member");
            1 == a.globalData.store_alone_temp && (o = a.getNetAddresss("plugin.store-alone-temp.frontend.coupon.coupons-for-member")), 
            this.data.page >= this.data.total_page ? t.setData({
                loading: !0,
                allLoaded: !0
            }) : (t.setData({
                page: this.data.page + 1
            }), o += "&page=" + t.data.page, o += "&coupon_type=" + t.data.typesIndex, a._getNetWork({
                url: o,
                success: function(a) {
                    var e = a.data;
                    if (1 == e.result) {
                        for (var o = e.data, n = 0; n < o.data.data.length; n++) {
                            var s = o.data.data[n];
                            s.isIntegralExchange = 0 != s.is_integral_exchange_coupon && s.exchange_coupon_integral > 0, 
                            "平台商品" == s.coupon_type_name ? s.type = 1 : "兑换券" == s.coupon_type_name ? s.type = 2 : "分类商品" == s.coupon_type_name ? s.type = 3 : "指定商品" == s.coupon_type_name ? s.type = 4 : "酒店商品" == s.coupon_type_name ? s.type = 5 : "门店商品" == s.coupon_type_name ? s.type = 6 : "商品和门店" == s.coupon_type_name && (s.type = 8);
                        }
                        e.data.coupon_form ? t.setData({
                            coupon_form: e.data.coupon_form
                        }) : t.setData({
                            coupon_form: ""
                        }), t.setData({
                            loading: !1,
                            allLoaded: !1,
                            coupon_list: t.data.coupon_list.concat(o.data.data)
                        });
                    } else t.setData({
                        page: t.page - 1,
                        loading: !0,
                        allLoaded: !0,
                        isLoadMore: !1
                    });
                },
                fail: function(a) {
                    console.log(a);
                }
            }));
        } else this.setData({
            loading: !0,
            allLoaded: !0,
            isLoadMore: !1
        });
    },
    claim: function(a) {
        console.log(a);
        var e = a.detail.index, t = a.detail.item, o = a.detail.item.use_type;
        this.selectedcoupon(o, t, e);
    },
    tabrshowbtn: function(a) {
        a.detail && this.setData({
            kFootshow: !1
        });
    },
    showIntegralPopup: function(a) {
        var e = a.detail.index, t = a.detail.item, o = a.detail.item.use_type, n = {
            couponTotal: 1,
            exIntegral: t.exchange_coupon_integral
        };
        this.setData({
            exchangeForm: n,
            visibleIntegralPopup: !0,
            callbackFn: this.selectedcoupon.bind(this, o, t, e)
        });
    },
    changeTotal: function(a) {
        this.setData({
            "exchangeForm.couponTotal": a.detail
        });
    },
    cancelExchange: function() {
        this.setData({
            visibleIntegralPopup: !1
        });
    },
    exchangeCoupon: function() {
        if (this.data.exchangeForm.couponTotal <= 0) return wx.showToast({
            title: "请输入兑换优惠券的数量！"
        });
        this.data.callbackFn(), this.setData({
            visibleIntegralPopup: !1
        });
    }
});